import axios from "axios";
import * as HTTP_CONSTANT from "./constant"

const instance = axios.create({
  baseURL: import.meta.env.VITE_BASE_API,
  timeout: HTTP_CONSTANT.TIMEOUT,
  withCredentials: true,
})

console.log('import.meta.env', import.meta.env);
// 请求拦截器
instance.interceptors.request.use(
  (config) => {
    let token = "";
    token = sessionStorage.getItem("token");
    if (token) {
      config.headers["Authorization"] = "Bearer " + token;
    }

    config.headers["Content-Type"] = "application/json";
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  (response) => {
    if (response.status == 200) {
      return Promise.resolve(response);
    } else {
      return Promise.reject(response);
    }
  },
  (error) => {
      console.log("后端接口连接异常", error);
      return Promise.reject(error.response);
  }
);

export default instance;
